require.config({
    baseUrl: 'js/common',
    paths: {
        "deptData": "../data/deptData",
        "zhuguanData": "../data/zhuguanData",
        "stuData": "../data/stuData",
        "pagin": "./pagin",
        "common": "./common"
    }
});
require(['pagin', 'zhuguanData', 'stuData', 'deptData', 'common'], function(pagin, ZG, Stu, Depts, Common) {
    var baseUrl = ZG.baseUrl;
    /*
     *------助管信息管理
     *增加助管：addZhuguan
     *删除助管：deleteZG
     *查询数据：queryData
     *向服务器获取所有数据：getAllData
     *向服务器查询数据（页）：getPage
     *获取学院信息列表，获取银行列表：getDept,getBanks，目前是写死的
     *说明：默认每页显示5条记录
     *初始化，向服务器获取前5条记录，以及总的记录条数，再更新分页
     *-------
     */
    var zhuguan_info = new Vue({
        el: '#zhuguan-data',
        data: {
            /*查询对象*/
            query: {
                "rows": 10,
                "page": 1,
            },
            stu_query: {
                "rows": 10,
                "page": 1
            },
            check_deptId: [],
            check_stuDeptId: [],
            /*pageNum 总页数*/
            pageNum: 1,
            /*当前页码*/
            curPage: 1,
            /*pages用于构造page*/
            pages: [],
            /*pageNum 总页数*/
            stu_pageNum: 1,
            /*当前页码*/
            stu_curPage: 1,
            /*pages用于构造page*/
            stu_pages: [],
            /*用于提取编辑学生*/
            stu_edit: '',
            /*部门列表*/
            depts: [],
            /*银行列表*/
            banks: [{
                "bankNo": "1",
                "bankName": "中国银行"
            }, {
                "bankNo": "2",
                "bankName": "建设银行"
            }],
            /*列表中的助管项目*/
            items: [],
            /*所有学生*/
            stu_items: [],
            /*选中删除的助管对象*/
            deleteItem: '',
            /*要添加的助管对象*/
            addItem: '',
            currentView: '',
            total: 0,
            check_stuId: [],
            check_AllDept: false,
            url: {
                "uploadFile": baseUrl + '/base/stu/upload.do',
                "downloadMB": baseUrl + '/base/stu/downloadMB.do'
            }
        },
        methods: {
            /*初始化*/
            init: function() {
                var _self = this;
                // this.getBanks();
                _self.queryData();
                _self.getDepts();
                _self.queryStuData();
            },
            /*查询数据*/
            queryData: function(str) {
                var _self = this;
                if (str == "page1") {
                    _self.query = [];
                    _self.query = {
                        "rows": 10,
                        "page": 1,
                    };
                    _self.curPage = 1;
                }
                if (str == "all") {
                    _self.query.page = 1;
                    _self.curPage = 1;
                }
                _self.query.stuId = _self.query_stuId;
                _self.query.name = _self.query_name;
                _self.query.deptId = Common.arrayToStr(_self.check_deptId);
                ZG.queryZG(_self.query).done(function(result) {
                    _self.total = result.total;
                    _self.pageNum = Math.ceil(result.total / _self.query.rows);
                    _self.items = [];
                    $.each(result.result, function(i, value) {
                        _self.items.push(value);
                    });
                    /*更新分页*/
                    var result = pagin.paginGetli(_self.pageNum, _self.curPage);
                    _self.pages = result.pages;
                })

            },
            /*查询学生数据*/
            queryStuData: function(str) {
                var _self = this;
                if (str) {
                    _self.stu_query.page = 1;
                    _self.stu_curPage = 1;
                }
                // _self.stu_query.deptId=Common.arrayToStr(_self.check_stuDeptId);

                Stu.queryStu(_self.stu_query).done(function(result) {
                    _self.stu_total = result.total;
                    _self.stu_pageNum = Math.ceil(result.total / _self.stu_query.rows);
                    _self.stu_items = [];
                    $.each(result.result, function(i, value) {
                        _self.stu_items.push(value);
                    });
                    /*更新分页*/
                    var result = pagin.paginGetli(_self.stu_pageNum, _self.stu_curPage);
                    _self.stu_pages = result.pages;
                })
            },
            /*根据row和page请求数据*/
            getDataPage: function(cur_page) {
                var _self = this;
                _self.query.page = cur_page;
                _self.curPage = cur_page;
                _self.queryData();
            },
            /*处理助管列表分页查询*/
            submitPage: function(p, b) {
                var _self = this;
                console.log(p);
                if (b) {
                    return false;
                }
                if (p === ">>") {
                    _self.curPage++;
                    _self.getDataPage(_self.curPage);
                } else if (p === "<<") {
                    _self.curPage--;
                    _self.getDataPage(_self.curPage);
                } else if (p === "...") {
                    if (_self.curPage > _self.pageNum) {
                        _self.getDataPage(_self.pageNum);
                    } else {
                        _self.getDataPage(_self.curPage + 5);
                    }

                } else {
                    _self.curPage = p;
                    _self.getDataPage(_self.curPage);
                }
            },
            /*根据row和page请求数据*/
            getStuPage: function(p) {
                var _self = this;
                console.log(p);
                _self.stu_query.page = p;
                _self.stu_curPage = p;
                _self.queryStuData();
            },
            /*处理学生列表分页查询*/
            submitStuPage: function(p) {
                var _self = this;
                console.log(p);
                if (p === ">>") {
                    _self.stu_curPage++;
                    _self.getStuPage(_self.stu_curPage);
                } else if (p === "<<") {
                    _self.stu_curPage--;
                    _self.getStuPage(_self.stu_curPage);
                } else if (p === "...") {
                    if (_self.stu_curPage > _self.stu_pageNum) {
                        _self.getStuPage(_self.stu_pageNum);
                    } else {
                        _self.getStuPage(_self.stu_curPage + 5);
                    }

                } else {
                    _self.stu_curPage = p;
                    _self.getStuPage(_self.stu_curPage);
                }
            },
            /*获取部门名称以及列表*/
            getDepts: function() {
                var _self = this;
                var query = {
                    "rows": 100,
                    "page": 1
                };
                _self.depts = [];
                Depts.queryDepts(query).done(function(result) {
                    $.each(result.result, function(i, value) {
                        _self.depts.push(value);
                    })
                });
            },
            /*添加学生为助管*/
            addZhuguan: function() {
                var _self = this;
                var id = '';
                var len = _self.check_stuId.length;
                for (i in _self.check_stuId) {
                    if (i == len - 1) {
                        id = id + _self.check_stuId[i];
                    } else {
                        id = id + _self.check_stuId[i] + ',';
                    }
                }
                var data = {
                    "stuIds": id
                };
                ZG.addZG(data).done(function(result) {
                    alert(result.message);
                    // _self.queryData('page1');
                    $('#myModal-add').modal('hide')
                    location.reload();

                });
            },
            /*删除一条助管信息*/
            deleteZG: function(i, name) {
                var _self = this;
                _self.deleteItem = {
                    "stuID": i,
                    "name": name
                };
            },
            confirmDel: function() {
                var _self = this;
                var deleteItem = {
                    "stuID": _self.deleteItem.stuID
                };
                ZG.delZG(deleteItem).done(function(result) {
                    alert(result.message);
                    // _self.queryData('page1');
                    $('#myModal-delete').modal('hide')
                    location.reload();
                });
            },
            /*编辑一条助管信息*/
            editZG: function(i, name) {
                var _self = this;
                _self.stu_edit = {
                    "stuId": i,
                    "name": name
                };
            },
            confirmEdit: function() {
                var _self = this;
                ZG.editZG(_self.stu_edit).done(function(result) {
                    alert(result.message);
                    _self.queryData();
                    $('#myModal-modify').modal('hide')
                })
            },
            /*上传文件*/
            upload: function() {
                var formData = new FormData();
                formData.append('uploadExcel', $('#file')[0].files[0]);
                $.ajax({
                    url: this.url.uploadFile,
                    type: 'POST',
                    cache: false,
                    data: formData,
                    processData: false,
                    contentType: false
                }).done(function(res) {
                    alert('提交数据成功')
                    location.reload();

                }).fail(function(res) {
                    alert('提交数据失败')
                });
            },
            checkAllDept: function() {
                var _self = this;
                console.log('check_Allmonth');
                _self.check_deptId = [];
                if (!_self.check_AllDept) {
                    $.each(_self.depts, function(i, value) {
                        _self.check_deptId.push(value.deptId);
                    })
                };
                _self.queryData()

            }

        },
        watch: {
            check_deptId: function(val) {
                if (val.length < this.depts.length) {
                    this.check_AllDept = false;
                } else {
                    this.check_AllDept = true;
                }
                this.queryData('all')
            },
            query_stuId: function(val) {
                this.queryData('all')
            },
            query_name: function(val) {
                this.queryData('all')
            }
        }

    });
    zhuguan_info.init();
});